import { GameMode } from "@minecraft/server";
import { PlayerGroup } from "@sapi-game/gamePlayer/playerGroup";
import { eleConfigManager } from "./configManager";
import { ElevatorCatContext } from "./context";
import { eleMainState } from "./mainState/state";
import { elevatorCatModule } from "./module";
import { AutoStopState } from "@sapi-game/gameState/common/autoStop";

interface elevatorGameConfig {
    cat: PlayerGroup;
    mouse: PlayerGroup;
}

export class elevatorCatGame extends elevatorCatModule.Engine<elevatorGameConfig> {
    protected override buildContext(config: elevatorGameConfig): ElevatorCatContext {
        const cat = this.groupBuilder.fromGroup(config.cat);
        const mouse = this.groupBuilder.fromGroup(config.mouse);
        return new ElevatorCatContext(cat, mouse, eleConfigManager.getConfig());
    }

    override onStart(): void {
        this.pushState(AutoStopState, { groupSet: this.context.group });
        this.pushState(eleMainState);
    }

    override onStop(): void {
        this.context.group.forEach((p) => {
            if (!p.player) return;
            p.player.teleport({ x: 369.0, y: -59.0, z: 159.0 }, { rotation: { x: 0, y: 180 } });
            p.player.setGameMode(GameMode.Adventure);
            p.player.runCommand("clear @s");
            p.player.runCommand("effect @s clear");
            p.player.camera.clear();
        });
    }
}
